home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Taifun / Taifun 009 (1987-02-15)(Ossowski, Stefan)(DE)(PD).zip / Taifun 009 (1987-02-15)(Ossowski, Stefan)(DE)(PD).adf / HackIconII_Source / EditIcon.c < prev    next >
C/C++ Source or Header  |  1987-03-04  |  13KB  |  442 lines

  1. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
  2. * |_o_o|\\ Copyright (c) 1987 The Software Distillery.  All Rights Reserved *
  3. * |. o.| ||          Written by Doug Walker                                 *
  4. * | .  | ||          The Software Distillery                                *
  5. * | o  | ||          235 Trillingham Lane                                   *
  6. * |  . |//           Cary, NC 27511                                         *
  7. * ======             BBS:(919)-471-6436                                     *
  8. \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  9.  
  10. #include "mydebug.h"
  11.  
  12. #include "exec/types.h"
  13. #include "exec/memory.h"
  14. #include "intuition/intuition.h"
  15. #include "graphics/gfxbase.h"
  16. #include "stdio.h"
  17.  
  18. #include "hackicon.h"
  19.  
  20. extern struct IntuitionBase *IntuitionBase;
  21. extern struct GfxBase *GfxBase;
  22.  
  23. extern struct IntuiText MiscText[];
  24. extern struct Menu Titles[], EditTitles[], ORCTitles[];
  25. extern struct Gadget colgads[];
  26.  
  27. extern struct Screen   *Screen;
  28. extern struct Window   *Window;
  29. extern struct ViewPort *ViewPort;
  30.  
  31. extern struct Border Borders[];
  32.  
  33. extern struct iconfont *fonts[2];
  34. extern int fontnum,hires;
  35. extern USHORT colormap[];
  36.  
  37. char *hexchars = "0123456789ABCDEF";
  38.  
  39. void DrawEdit(EditWindow, tempdata)
  40. struct Window *EditWindow;
  41. USHORT *tempdata;
  42. {
  43.    short x, y, ix, iy, im, mask, xmask;
  44.    static int colornum[16]    = {0, 8, 4, 12, 2, 10, 6, 14,
  45.                                  1, 9, 5, 13, 3, 11, 7, 15};
  46.    int tmpint;
  47.  
  48.    for(x=EDXLEFT, ix=0; ix<IWIDTH; x+=ELWIDTH, ix++)
  49.    {
  50.       xmask = 0x8000 >> ix;
  51.       for(y=EDYTOP, iy=0; iy<IHEIGHT; y+=ELHEIGHT, iy++)
  52.       {
  53.          tmpint = iy;
  54.          for(mask=im=0; im<IDEPTH; im++, tmpint+=IHEIGHT)
  55.          {
  56.             mask |= ((xmask & tempdata[tmpint]) >> (12-ix+im));
  57.          }
  58.          SetAPen(EditWindow->RPort,colornum[mask]);
  59.          RectFill(EditWindow->RPort, x, y, x+ELWIDTH-1, y+ELHEIGHT-1);
  60.       }
  61.    }
  62. BUG(1, ("DrawEdit: Exit\n"))
  63. }
  64.  
  65. void FlipX(data)
  66. register USHORT *data;
  67. {
  68.    int offset, x;
  69.    USHORT tmpdata[ILENGTH];
  70.  
  71.    movmem(data, tmpdata, ILENGTH*sizeof(USHORT));
  72.    setmem(data, ILENGTH*sizeof(USHORT), 0);
  73.    for(offset = 0; offset < ILENGTH; offset++)
  74.    {
  75.       for(x=0; x<IWIDTH; x++)
  76.       {
  77.          if(tmpdata[offset] & (0x8000 >> x))
  78.          data[offset] |= (0x8000 >> (IWIDTH-1 - x));
  79.       }
  80.    }
  81. }
  82.  
  83. void FlipY(data)
  84. register USHORT *data;
  85. {
  86.    int depth, offset, y;
  87.    USHORT tmpdata[ILENGTH];
  88.    
  89.    movmem(data, tmpdata, ILENGTH*sizeof(USHORT));
  90.    setmem(data, ILENGTH*sizeof(USHORT), 0);
  91.    for(offset=depth=0; depth<IDEPTH; depth++, offset+=IHEIGHT)
  92.       for(y=0; y<IHEIGHT; y++)
  93.          data[offset+y] = tmpdata[offset+IHEIGHT-1-y];
  94. }
  95.  
  96. void Rotate(data, plus)
  97. register USHORT *data;
  98. int plus;
  99. {
  100.    USHORT mask;
  101.    int depth, off1, off2, x, y;
  102.    USHORT tmpdata[ILENGTH];
  103.  
  104.    movmem(data, tmpdata, ILENGTH*sizeof(USHORT));
  105.    setmem(data, ILENGTH*sizeof(USHORT), 0);
  106.    for(depth=off1=0; depth<IDEPTH; depth++, off1+=IHEIGHT)
  107.    {
  108.       mask = (plus ? 0x8000 >> (IWIDTH-1): 0x8000);
  109.       for(off2=off1, y=0; y<IHEIGHT; off2++, y++)
  110.       {
  111.          for(x=0; x<IWIDTH; x++) 
  112.      {
  113.         if(tmpdata[off1+x] & mask)
  114.                data[off2] |= (plus ? (0x8000 >> x)  
  115.                                    :(0x8000 >> (IWIDTH-1-x) ) );
  116.          }
  117.          mask = (plus ? mask << 1 : mask >> 1);
  118.       }
  119.    }
  120. }
  121.  
  122. void UDShift(data, up)
  123. register USHORT *data;
  124. int up;
  125. {
  126.    register int end, incr, y, offset;
  127.    int start;
  128.  
  129.    if(up)
  130.    {
  131.       start = 0;
  132.       end   = IHEIGHT-1;
  133.       incr  = 1;
  134.    }
  135.    else
  136.    {
  137.       start = IHEIGHT-1;
  138.       end   = 0;
  139.       incr  = -1;
  140.    }
  141.  
  142.    for(offset=0; offset<ILENGTH; offset+=8)
  143.    {
  144.       for(y=start; y!=end; y+=incr)
  145.          data[offset + y] = data[offset + y + incr];
  146.  
  147.       data[offset + y] = 0;
  148.    }
  149. }
  150.  
  151. void LRShift(data, left)
  152. register USHORT *data;
  153. int left;
  154. {
  155.    int i;
  156.    
  157.    if(left) for(i=0; i<ILENGTH; i++) data[i] <<= 1;
  158.    else     for(i=0; i<ILENGTH; i++)
  159.    {
  160.       data[i] >>= (16 - IWIDTH + 1);
  161.       data[i] <<= (16 - IWIDTH);
  162.    }
  163. }
  164.  
  165.  
  166. EditIcon(which)
  167. int which;
  168. {
  169.    register struct Image *tempimage, *update;
  170.    register USHORT *tempdata;
  171.  
  172.    struct NewWindow *NewWindow;
  173.    struct Window *EditWindow;
  174.    struct Gadget *Gadget;
  175.    struct IntuiMessage *imsg;
  176.    short draw, inum, tmpint;
  177.    char WindowTitle[10];
  178.    int editmode;
  179.  
  180.    USHORT x, y, Code, curcolor;
  181.    ULONG Class, mask;
  182.  
  183.    BUG(1, ("EditIcon: Enter, # %d\n", which))
  184.  
  185.    editmode = 1;
  186.    curcolor = colormap[0];
  187.    if(!fonts[fontnum]) LoadFont("Characters", -1);
  188.    if(fonts[fontnum]->firstchar > which) fonts[fontnum]->firstchar = which;
  189.    if(fonts[fontnum]->lastchar  < which) fonts[fontnum]->lastchar  = which;
  190.  
  191.    /* Disable Menus */
  192.    ClearMenuStrip(Window);
  193.  
  194.    if(!(NewWindow = (struct NewWindow *)
  195.             AllocMem(sizeof(struct NewWindow), MEMF_CHIP | MEMF_CLEAR))
  196.    || !(tempimage = (struct Image *)
  197.             AllocMem(sizeof(struct Image), MEMF_CHIP | MEMF_CLEAR))
  198.    || !(tempdata = (USHORT *)
  199.             AllocMem(ILENGTH*sizeof(USHORT), MEMF_CHIP | MEMF_CLEAR))
  200.      )
  201.    {
  202.       AutoRequest(Window, &MiscText[3], NULL, &MiscText[2], 0, 0, 410, 90);
  203.       return(1);
  204.    }
  205.  
  206.    NewWindow->LeftEdge = SWIDTH/4;
  207.    NewWindow->TopEdge = SHEIGHT/4;
  208.    NewWindow->Width  = CESWIDTH;
  209.    NewWindow->Height = CESHEIGHT;
  210.    NewWindow->DetailPen = 1;
  211.    NewWindow->BlockPen  = 2;
  212.    NewWindow->IDCMPFlags  = (MOUSEBUTTONS  | MOUSEMOVE | GADGETUP | MENUPICK);
  213.    NewWindow->Flags = (SMART_REFRESH | ACTIVATE | REPORTMOUSE | WINDOWDRAG);
  214. #if 0
  215.    NewWindow->Title = (UBYTE *)"Edit Window";
  216. #else
  217.    NewWindow->Title = WindowTitle;
  218.    strcpy(NewWindow->Title, "Edit ' '");
  219.    if(which>33 && which<126) 
  220.       NewWindow->Title[6] = which;
  221.    else
  222.    {
  223.       NewWindow->Title[5] = '$';
  224.       NewWindow->Title[6] = hexchars[(UBYTE)((0xf0 & (UBYTE)(which)) >> 4)];
  225.       NewWindow->Title[7] = hexchars[(UBYTE)(0x0f & which)];
  226.    }
  227. #endif
  228.    NewWindow->Screen = Screen;
  229.    NewWindow->MinWidth  =
  230.    NewWindow->MaxWidth  = SWIDTH/2;
  231.    NewWindow->MinHeight =
  232.    NewWindow->MaxHeight = SHEIGHT/2;
  233.    NewWindow->Type = CUSTOMSCREEN;
  234.    NewWindow->FirstGadget = colgads;
  235.  
  236.    EditWindow = (struct Window *)OpenWindow(NewWindow);
  237. BUG(9, ("init: Window open\n"))
  238.  
  239.    FreeMem(NewWindow, sizeof(struct NewWindow));
  240. BUG(9, ("EditIcon: newwindow mem freed\n"))
  241.  
  242.    LoadRGB4(ViewPortAddress(EditWindow), colormap, 16);
  243. BUG(9, ("EditIcon: Colors loaded\n"))
  244.  
  245.    ORCTitles[0].NextMenu = EditTitles;
  246.    SetMenuStrip(EditWindow, ORCTitles);
  247. BUG(9, ("EditIcon: Menus set\n"))
  248.  
  249.    movmem((char *)(fonts[fontnum]->idata+(which*ILENGTH)),
  250.           (char *)tempdata,
  251.           ILENGTH*sizeof(USHORT));
  252. BUG(9, ("EditIcon: icon data moved\n"))
  253.  
  254.    tempimage->LeftEdge = EDXLEFT - 3*IWIDTH;
  255.    tempimage->TopEdge  = EDYTOP + (EDHEIGHT-IHEIGHT)/2;
  256.    tempimage->Width    = IWIDTH;
  257.    tempimage->Height   = IHEIGHT;
  258.    tempimage->Depth    = IDEPTH;
  259.    tempimage->ImageData = tempdata;
  260.    tempimage->PlanePick = 0x0F;
  261.    tempimage->PlaneOnOff= 0x00;
  262.    tempimage->NextImage = NULL;
  263. BUG(9, ("EditIcon: tempimage initialized\n"))
  264.  
  265.    DrawBorder(EditWindow->RPort, Borders, 0, 0);
  266.    if(hires) DrawBorder(EditWindow->RPort, &Borders[1], PLXWIDTH+3, 0);
  267.    DrawImage(EditWindow->RPort, tempimage, 0, 0);
  268.    DrawEdit(EditWindow, tempdata);
  269.  
  270.    draw = 0;
  271.  
  272.    while(editmode)
  273.    {
  274.       if(!(imsg = (struct IntuiMessage *)GetMsg(EditWindow->UserPort)))
  275.       {
  276.          WaitPort(EditWindow->UserPort);
  277.          continue;
  278.       }
  279.       Class  = imsg->Class;
  280.       Code   = imsg->Code;
  281.       x      = imsg->MouseX;
  282.       y      = imsg->MouseY;
  283.       Gadget = (struct Gadget *)imsg->IAddress;
  284.       ReplyMsg(imsg);
  285. BUG(9, ("EditIcon: Message class is %d\n", Class))
  286.  
  287.       switch(Class)
  288.       {
  289.  
  290.          case MOUSEBUTTONS:
  291.             BUG(1, ("EditIcon: case MOUSEBUTTONS\n"))
  292.             if(Code == SELECTUP)
  293.             {
  294.                draw = 0;
  295.                break;
  296.             }
  297.             draw = 1;
  298.             /* Fall through to MOUSEMOVE */
  299.  
  300.          case MOUSEMOVE:
  301.             if(!draw
  302.             || x < EDXLEFT || x >= (EDXLEFT+EDWIDTH)
  303.             || y < EDYTOP  || y >= (EDYTOP+EDHEIGHT)) break;
  304.  
  305.             mask = 0x8000 >> (x = (x - EDXLEFT)/ELWIDTH);
  306.             tmpint = y = (y - EDYTOP)/ELHEIGHT;
  307.  
  308.             for(inum=0; inum<IDEPTH; inum++, tmpint+=IHEIGHT)
  309.             {
  310.                if(curcolor & (0x0001 << inum) ) tempdata[tmpint] |= mask;
  311.                else tempdata[tmpint] &= (~mask);
  312.             }
  313.             DrawImage(EditWindow->RPort, tempimage, 0, 0);
  314.             x = x*ELWIDTH + EDXLEFT;
  315.             y = y*ELHEIGHT + EDYTOP;
  316.             SetAPen(EditWindow->RPort, curcolor);
  317.             RectFill(EditWindow->RPort, x, y, x+ELWIDTH-1, y+ELHEIGHT-1);
  318.  
  319.             break;
  320.  
  321.          case GADGETUP:
  322.             curcolor = Gadget->GadgetID;
  323.             BUG(5, ("EditIcon: Color gadget # %d\n", curcolor))
  324.             break;
  325.             
  326.          case MENUPICK:
  327.             BUG(5, ("EditIcon: case MENUPICK\n"))
  328.             if(Code == MENUNULL) break;
  329.  
  330. BUG(8, ("EditIcon: menu=%d, item=%d, sub=%d\n", MENUNUM(Code),ITEMNUM(Code), SUBNUM(Code)))
  331.  
  332.             if(!MENUNUM(Code))
  333.             {
  334.                switch(ITEMNUM(Code))
  335.                {
  336.                   case ME_SAVE:
  337.                      BUG(5, ("EditIcon: Save\n"))
  338.                      movmem((char *)tempdata,
  339.                             (char *)(fonts[fontnum]->idata+(which*ILENGTH)),
  340.                             ILENGTH*sizeof(USHORT));
  341.                      update = fonts[fontnum]->images + which;
  342.                      update->NextImage = NULL;
  343.                      DrawImage(Window->RPort, update, 0, 0);
  344.                      fonts[fontnum]->flags |= CHANGED;
  345.                      if(which != 255) update->NextImage = update+1;
  346.                      editmode = 0;
  347.                      break;
  348.  
  349.                   case ME_QUIT:
  350.                      BUG(5, ("EditIcon: Cancel\n"))
  351.                      editmode = 0;
  352.                      break;
  353.                      
  354.                   case ME_RESET:
  355.                      BUG(5, ("EditIcon: Reset\n"))
  356.                      movmem((char *)(fonts[fontnum]->idata+(which*ILENGTH)),
  357.                             (char *)tempdata,
  358.                             ILENGTH*sizeof(USHORT));
  359.                      DrawImage(EditWindow->RPort, tempimage, 0, 0);
  360.                      DrawEdit(EditWindow, tempdata);
  361.                      break;
  362.                }
  363.                break;
  364.             }
  365.             switch(ITEMNUM(Code))
  366.             {
  367.                case ME_FLIP:
  368.                   switch(SUBNUM(Code))
  369.                   {
  370.                      case ME_XFLIP:
  371.                         FlipX(tempdata);
  372.                         break;
  373.                      case ME_YFLIP:
  374.                         FlipY(tempdata);
  375.                         break;
  376.                   }
  377.                   break;
  378.  
  379.                case ME_ROTATE:
  380.                   switch(SUBNUM(Code))
  381.                   {
  382.                      case ME_180ROT:
  383.                         Rotate(tempdata, 1);
  384.                      case ME_POSROT:
  385.                         Rotate(tempdata, 1);
  386.                         break;
  387.                      case ME_NEGROT:
  388.                         Rotate(tempdata, 0);
  389.                         break;
  390.                   }
  391.                   break;
  392.  
  393.                case ME_SHIFT:
  394.                   switch(SUBNUM(Code))
  395.                   {
  396.                      case ME_LSHIFT:
  397.                         LRShift(tempdata, 1);
  398.                         break;
  399.  
  400.                      case ME_RSHIFT:
  401.                         LRShift(tempdata, 0);
  402.                         break;
  403.                   
  404.                      case ME_USHIFT:
  405.                         UDShift(tempdata, 1);
  406.                         break;
  407.                   
  408.                      case ME_DSHIFT:
  409.                         UDShift(tempdata, 0);
  410.                         break;
  411.                   }
  412.                   break;
  413.                   
  414.             }
  415.             DrawImage(EditWindow->RPort, tempimage, 0, 0);
  416.             DrawEdit(EditWindow, tempdata);
  417.             break;
  418.  
  419.          default:
  420.             BUG(1, ("EditIcon: Unknown Class = %d\n", Class))
  421.             break;
  422.       }
  423.    }
  424.  
  425.    /* Clear the regular window's messages */
  426.    while(imsg = (struct IntuiMessage *)GetMsg(Window->UserPort))
  427.       ReplyMsg(imsg);
  428.  
  429.    /* Enable Menus */
  430.    ClearMenuStrip(EditWindow);
  431.    SetMenuStrip(Window, Titles);
  432.  
  433.    CloseWindow(EditWindow);
  434.    FreeMem(tempdata, ILENGTH*sizeof(USHORT));
  435.    FreeMem(tempimage, sizeof(struct Image));
  436.  
  437.    BUG(1, ("EditIcon: Exit\n"))
  438.    return(0);
  439. }
  440.  
  441.  
  442.